package com.minhua.jijiehao2.mainframe.mine.productListFragment.impl

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseLazyFragment
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.ProductBean
import com.minhua.jijiehao2.mainframe.mine.productListFragment.imp.IProductListView
import kotlinx.android.synthetic.main.fragment_product_list.*

class ProductListFragment : BaseLazyFragment<IProductListView, ProductListPresenter>(), IProductListView {
    override fun onFirstUserVisible() {
        refresh.isRefreshing = true
        mPresenter.getProductList(auditState)
    }

    override fun onUserVisible() {
        mPresenter.getProductList(auditState)
    }

    override fun onUserInvisible() {
    }

    override fun getLayoutId(): Int = R.layout.fragment_product_list

    override fun getPresenter(): ProductListPresenter = ProductListPresenter()

    companion object {
        fun newInstance(position: Int): ProductListFragment {
            val fragment = ProductListFragment()
            val args = Bundle()
            args.putInt("auditState", position)
            fragment.arguments = args
            return fragment
        }
    }

    private var auditState = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        auditState = arguments?.getInt("auditState") ?: 1
    }


    private var adapter: BaseCommonAdapter<ProductBean>? = null
    private val listData = arrayListOf<ProductBean>()
    override fun initView() {
        refresh.setOnRefreshListener { mPresenter.getProductList(auditState) }
        adapter = object : BaseCommonAdapter<ProductBean>(activity, R.layout.item_product_list, listData) {
            override fun convert(holder: ViewHolder, t: ProductBean) {
                Glide.with(activity).load(t.logo_path).into(holder.getView(R.id.ivIcon))
                holder.setText(R.id.tvItemName, t.proc_name)
                holder.setText(R.id.tvItemTag, t.companyInfo.registeredCapital + "万注册资金")
                holder.setText(R.id.tvItemMessage, t.proc_info)
                holder.setText(R.id.tvTime, "上架时间：" + t.create_time)
            }
        }
        rvContent.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        rvContent.adapter = adapter
    }

    override fun onListSuccess(list: List<ProductBean>) {
        refresh.isRefreshing = false
        listData.clear()
        listData.addAll(list)
        adapter?.notifyDataSetChanged()
    }
}
